﻿using System;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChoViet.CMS.Properties;
using ChoViet.CMS.ViewModels;
using ChoViet.Lib.Common;
using ChoViet.Lib.Helpers;
using ChoViet.Model;
using ChoViet.Lib.Paging;
using System.Linq.Dynamic;
namespace ChoViet.CMS.Controllers
{
    [RoleHelper(Roles = RoleSystem.ArticleCategory)]
    public class ArticleTypeController : BaseController
    {

        /// <summary>
        /// Created by Vietlv
        /// Created date 3/05/2014
        /// Description: Manage ArticleType- Class ArticleCategory controler;
        /// </summary>

        #region GET: /ArticleCategory/IndexCms
        public ActionResult IndexCms(int? page)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
             if (page == null) page = 1;
            try
            {
                if (UserManager.CheckRole(RoleSystem.Dictionary))
                {
                    string order = UserState.getUserStateFromRequest("category.order", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("category.orderDir", "orderDir", "desc").ToString();
                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;
                    int pageSize = int.Parse(UserState.getUserStateFromRequest("article.pageSize", "PageSize", Settings.Default.PageSize).ToString());
                    ArticleTypeIndexViewModel model = new ArticleTypeIndexViewModel();
                    model.ArticleTypes = eDataContext.ArticleTypes.Where(e => e.ID > 0)
                                                .OrderBy(string.Format("{0} {1}", order, orderDir))
                                                .ToPagedList(page.Value - 1, pageSize);
                    return View(model);
                }
                else
                {
                    Messages.AddFlashMessage("Bạn không có quyền truy cập ứng dụng này");
                    ViewData["MessageInfo"] = "Bạn không có quyền truy cập ứng dụng này";
                    return View();
                }
            }
            catch (Exception exp)
            {
                eDataContext.Dispose();
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }
        #endregion

        #region GET: /ArticleCategory/DetailsCms
        public ActionResult DetailsCms(int id)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            var param = eDataContext.ArticleCategories.FirstOrDefault(e => e.ID == id);
            return View(param);
        }
        #endregion

        #region GET: /ArticleCategory/CreateCms
        public ActionResult CreateCms()
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                ArticleTypeEditViewModel model= new ArticleTypeEditViewModel();    
                return View(model);
            }
            finally { eDataContext.Dispose(); }
        }
        #endregion

        #region POST: /ArticleCategory/CreateCms
        [HttpPost][ValidateInput(false)]
        public ActionResult CreateCms(ArticleTypeEditViewModel c)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                if (eDataContext.Connection.State == System.Data.ConnectionState.Closed) eDataContext.Connection.Open();
                eDataContext.Transaction = eDataContext.Connection.BeginTransaction();
                UpdateModel(c);
                eDataContext.ArticleTypes.InsertOnSubmit(c.ArticleType);
                eDataContext.SubmitChanges();
                eDataContext.Transaction.Commit();
                Messages.AddFlashMessage("Thêm loại chuyên mục thành công!");
                return RedirectToAction("IndexCms");
            }
            catch
            {
                eDataContext.Transaction.Rollback();
                eDataContext.Dispose();
                return View();
            }
            finally {
                eDataContext.Dispose();
            }
        }
        #endregion

        #region GET: /ArticleType/EditCms/5
        public ActionResult EditCms(int id)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                var param = new ArticleTypeEditViewModel();
                param.ArticleType = eDataContext.ArticleTypes.Where(e => e.ID == id).SingleOrDefault();
                return View(param);
            }
            catch (Exception exp)
            {
                eDataContext.Dispose();
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
            finally {
                eDataContext.Dispose();
            }
        }
        #endregion

        #region POST: /ArticleCategory/EditCms/5
        [HttpPost]
        public ActionResult EditCms(int id, ArticleTypeEditViewModel c)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                if (eDataContext.Connection.State == System.Data.ConnectionState.Closed) eDataContext.Connection.Open();
                eDataContext.Transaction = eDataContext.Connection.BeginTransaction();
                c.ArticleType = eDataContext.ArticleTypes.Where(cat => cat.ID == id).SingleOrDefault();
                UpdateModel(c);
                eDataContext.SubmitChanges();
                eDataContext.Transaction.Commit();
                Messages.AddFlashMessage("Sửa loại chuyên mục bài viết thành công!");
                return RedirectToAction("IndexCms");
            }
            catch (Exception exp)
            {
                eDataContext.Transaction.Rollback();
                eDataContext.Dispose();
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
            finally { eDataContext.Dispose(); }
        }
        #endregion

        #region GET: /ArticleCategory/DeleteCms/5
        public ActionResult DeleteCms(int id)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                if (UserManager.CheckRole(RoleSystem.Administrator))
                {
                    var item = eDataContext.ArticleTypes.Single(e => e.ID == id);
                    return View(item);
                }
                else
                {
                    Messages.AddErrorFlashMessage("Bạn không có quyền truy cập ứng dụng này. Chức năng này chỉ có Administrator mới có quyền");                    
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }
        #endregion

        #region POST: /ArticleCategory/DeleteCms/5
        [HttpPost]
        public ActionResult DeleteCms(int id, FormCollection collection)
        {
            ChoVietDataContext eDataContext = new ChoVietDataContext();
            try
            {
                if (UserManager.CheckRole(RoleSystem.Administrator))
                {
                    var item = eDataContext.ArticleTypes.Single(e => e.ID == id);
                    if (item != null)
                    {
                        eDataContext.ArticleTypes.DeleteOnSubmit(item);
                        eDataContext.SubmitChanges();
                    }
                    return RedirectToAction("IndexCms");
                }
                else
                {
                    Messages.AddErrorFlashMessage("Bạn không có quyền truy cập ứng dụng này! Chức năng này chỉ có administrator mới có quyền xóa");
                    return View();
                }
            }
            catch (Exception exp)
            {
                ViewData["MessageInfo"] = exp.Message;
                return View();
            }
        }
        #endregion
    }
}
